package com.naodong.shenluntiku.mvp.model.error;

import android.content.Intent;

import com.naodong.shenluntiku.mvp.view.activity.GetVerCodeActivityAutoBundle;

import me.shingohu.man.util.ToastUtil;
import me.shingohu.man.util.UiUtils;

/**
 * Created by hujie on 17/5/8.
 */

public class DefaultResponseErrorListener implements ResponseErrorListener {


    @Override
    public void handleResponseError(ApiError apiError) {

        if (apiError.getStatusCode() == ErrorCode.UNAUTHORIZED) {
            //去登录
            ToastUtil.makeToast("登录过期,请重新登录");
            Intent intent = GetVerCodeActivityAutoBundle.builder().relogin(true).build(UiUtils.getContext());
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
            UiUtils.getContext().startActivity(intent);
        } else {
            handlerError(apiError);
        }

    }


    public void handleErrorCode202(ApiError apiError) {
        ToastUtil.makeToast(apiError.getMessage());
    }

    public void handleErrorCode204(ApiError apiError) {
        ToastUtil.makeToast(apiError.getMessage());
    }


    private void handlerError(ApiError apiError) {
        if (apiError.getStatusCode() == ErrorCode.CODE_202) {
            handleErrorCode202(apiError);
        } else if (apiError.getStatusCode() == ErrorCode.CODE_204) {
            handleErrorCode204(apiError);
        } else {
            ToastUtil.makeToast(apiError.getMessage());
        }

    }
}
